#!/bin/bash

# Report to Slack Webhook disk status if one of two thresholds are exceeded
# Please go to the following Slack link for details of setting up an Incoming Slack Webhook
# https://api.slack.com/incoming-webhooks

. "$HELLO_IT_SCRIPT_SH_LIBRARY/com.github.ygini.hello-it.scriptlib.sh"

function informIT {

        # Slack Webhook
        slack_webhook=$(defaults read /usr/local/etc/com.filewave.slack slack_webhook)
        slack_channel=$(defaults read /usr/local/etc/com.filewave.slack slack_channel)

	# Current FileWave client name, OS version and current printers
	filewave_client_name=$(scutil --get LocalHostName)
	client_version=$(defaults read /usr/local/sbin/FileWave.app/Contents/Info.plist  CFBundleLongVersionString)
	sw_version=$(sw_vers -productVersion)
	printer_info=$(lpstat -p | awk '{print $2}' | xargs -n1 lpq -P)

	# Get time to post into footer
	now=$(date +%s)

	function PrinterInk {

		json="{
			\"username\": \"Helpdesk\",
			\"icon_emoji\": \":computer:\",
			\"channel\": \"#${slack_channel}\",
			\"text\": \":warning: *Printer Ink Request: ${filewave_client_name}*\",
			\"attachments\": [
						{
						\"color\": \"#FFE100\",
						\"pretext\": \"*!!Printer Ink is Low!!*\",
						\"text\": \"*Request:* Please Instal New Cartridge\n$printer_info\",
						\"footer\": \"macOS: $sw_version, client: ${client_version}\",
						\"ts\": \"$now\"
					}
			]
		}"
	}

	PrinterInk

	# Post to slack
	curl -X POST --data-urlencode "payload=$json" "$slack_webhook"
}


function onClickAction {
    informIT $@
}

main $@

exit 0
